package ModelLayer;
import java.util.ArrayList; //The use of ArrayList is needed because there can be
//several copies for each DVD.
public class DVD
{
    private String dvd_id; //This is an id for the dvd and it is unique.
    private String title;
    private String artist;
    private String publicationDate;
    private ArrayList<Copy> copies;
    
    /** CONSTRUCTORS */
    public DVD (String dvd_id, String title, String artist, String publicationDate)
    {
        this.dvd_id = dvd_id;
        this.title = title;
        this.artist = artist;
        this.publicationDate = publicationDate;
        
        copies = new ArrayList<Copy>();
    }
    
    /** ACCESSORS */
    public String getDVDId()
    {
        return dvd_id;
    }
    public String getTitle()
    {
        return title;
    }
    public String getArtist()
    {
        return artist;
    }
    public String getPublicationDate()
    {
        return publicationDate;
    }
    public ArrayList<Copy> copiesCollection(){
        if(copies== null){
            System.out.println("No copies of DVD at the time.");
        }
         return copies;
    }
    public String showDetails()
    {
        String details = ("DVD_ID = " + getDVDId() + '\n' + "Title = " + getTitle() +
                        "Artist = " + getArtist() + '\n' + "PublicationDate = " +
                        getPublicationDate());
                        
             if(copies.size() == 0)
             {
                 details += "There are no copies of this DVD.";
             }
             for(Copy foundCopy : copies)
             {
                 details += ("Serial Number = " + foundCopy.getSerialNumber() +
                          '\n' + "Purchase Date = " + foundCopy.getPurchaseDate()
                          + '\n' + "Purchase Price = " + foundCopy.getPurchasePrice());
                          
             }
        return details;
    }
    
    /** MUTATORS */
    public void setId(String Id)
    {
        dvd_id = Id;
    }
    public void setTitle(String Title)
    {
        title = Title;
    }
    public void setArtist(String Artist)
    {
        artist = Artist;
    }
    public void setPublicationDate(String PublicationDate)
    {
        publicationDate = PublicationDate;
    }
    public void addCopy(Copy myCopy)
    {
        copies.add(myCopy);
    }
    public void deleteCopy(Copy myCopy)
    {
        copies.remove(myCopy);
    }
    public String toString() // returns a string with atributes of a dvd
    {
        String s="Title=" + getTitle()+'\n'+ "Artist=" +getArtist()+ '\n' +"ID="+getDVDId()+'\n' + "PublicationDate=" + getPublicationDate()+'\n';
        return s;
    }
    public Copy findCopy(String serialNumber) {
        for(int i=0; i<copies.size();i++){
            if(serialNumber.equals(copies.get(i).getSerialNumber())){
                return copies.get(i);
            }
        }
        return null;
    }
    public Copy findUnrentedCopy()
    {
        int index=0;
        boolean found= false;
        Copy c=null;
        while(!found && index<copies.size())
        {
            if ( !copies.get(index).isRented())
            {
            c=copies.get(index);
            copies.get(index).rentCopy();
            found=true;
            }
            index++;
        }
        if(c==null)
        System.out.println("There are no copies for dvd: " + getTitle()); 
        return c;
    }
}
